#include <stdio.h>
#include "lvgl_encoder.h"

static void encoder_init(void);
static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
//static void encoder_handler(void);

lv_indev_t * indev_encoder;

static int32_t encoder_diff;
static lv_indev_state_t encoder_state;
char Capure_Enconder_State = 0;

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{

    static lv_indev_drv_t indev_drv;

    /*------------------
     * Encoder
     * -----------------*/

    /*Initialize your encoder if you have*/
    encoder_init();

    /*Register a encoder input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_ENCODER;
    indev_drv.read_cb = encoder_read;
    indev_encoder = lv_indev_drv_register(&indev_drv);

    /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
     *add objects to the group with `lv_group_add_obj(group, obj)`
     *and assign this input device to group to navigate in it:
     *`lv_indev_set_group(indev_encoder, group);`*/

}

/*------------------
 * Encoder
 * -----------------*/

/*Initialize your keypad*/
static void encoder_init(void)
{
    /*Your code comes here*/
    Encoder_init();
}

/*Will be called by the library to read the encoder*/
static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
	Capure_Enconder_State = Encoder_Get_State();

    if(Capure_Enconder_State==3)	//编码器的按键
    	encoder_state = LV_INDEV_STATE_PR;	//按下
    else if (Capure_Enconder_State==2)	//编码器左转
    	encoder_diff--;
    else if (Capure_Enconder_State==1)	//编码器右转
    	encoder_diff++;
    else
    	encoder_state = LV_INDEV_STATE_REL;	//松开

    data->enc_diff = encoder_diff;
    data->state = encoder_state;

    encoder_diff = 0;
}
//
///*Call this function in an interrupt to process encoder events (turn, press)*/
//static void encoder_handler(void)
//{
//    /*Your code comes here*/
//
//    encoder_diff += 0;
//    encoder_state = LV_INDEV_STATE_REL;
//}











